package org.soulraven.games.memory.ui.game.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.soulraven.games.memory.engine.MemoryEngine;
import org.soulraven.games.memory.ui.game.DisplayableCard;

public class SelectionController implements ActionListener {

	private static final Logger LOGGER = Logger.getLogger(SelectionController.class);

	private DisplayableCard previousSelection = null;
	private MemoryEngine engine = null;
	private static final int FIRST_SELECTION_RESOLVER = 0;
	private static final int SECOND_SELECTION_RESOLVER = 1;
	private Map<Integer, ActionListener> resolvers = new HashMap<Integer, ActionListener>();
	private int currentResolver;

	public SelectionController(final MemoryEngine engine) {
		this.engine = engine;
		this.currentResolver = FIRST_SELECTION_RESOLVER;
		resolvers.put(FIRST_SELECTION_RESOLVER, new FirstSelectionResolver(this));
		resolvers.put(SECOND_SELECTION_RESOLVER, new SecondSelectionResolver(this));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		LOGGER.debug("Action performed");
		resolvers.get(currentResolver).actionPerformed(e);
	}

	/**
	 * @return the previousSelection
	 */
	DisplayableCard getPreviousSelection() {
		return previousSelection;
	}

	/**
	 * @param previousSelection the previousSelection to set
	 */
	void setPreviousSelection(final DisplayableCard previousSelection) {
		this.previousSelection = previousSelection;
	}

	void selectionFinished() {
		currentResolver = 1 - currentResolver;
	}

	void pairShown(final boolean match) {
		engine.pairShown(match);
	}

	void disableAllCards() {
		engine.disableAllCards();
	}

	void enableAllSelectableCards() {
		engine.enableAllSelectableCards();
	}


	void matchesNotFound() {
		engine.matchesNotFound();
	}

	void matchesFound() {
		engine.matchesFound();
	}
}
